//
//  ViewController.swift
//  StanfordCaculator
//
//  Created by 黄睿 on 16/10/22.
//  Copyright © 2016年 ipaynow. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet private weak var display: UILabel!
    
    // 是否是第一次输入的状态
    private var userInterfaceInTheMiddleTyping = false
    
    @IBAction func touchDigit(sender: UIButton) {
        let digit = sender.currentTitle!
        
        if userInterfaceInTheMiddleTyping{
            let textCurrentlyToDisplay = display.text!
            display.text = textCurrentlyToDisplay + digit
        }else{
            display.text = digit
            userInterfaceInTheMiddleTyping = true
        }
        
    }
    
    // 计算属性中转换数据类型
    private var displayValue : Double{
        get{
            return Double(display.text!)!
        }
        set{
            display.text = String(newValue)
        }
    }

    private var brain = CaculatorBrain()
    
    @IBAction private func performOperation(sender: UIButton) {
        brain.setOperand(displayValue)
        userInterfaceInTheMiddleTyping  = false
        if let mathmaticalSymbol = sender.currentTitle{
            brain.performOperation(mathmaticalSymbol)
        }
        displayValue = brain.result
    }
}

